
//DNA序列

#include <iostream>
#include <string>
#include <climits>
using namespace std;

int main() {
    string s = "";
    cin >> s;
    int N = 0;
    cin >> N;
    int front = 0,back = 0;
    int length = s.length();
    int cnt = 0; //用来记录G和C出现的总次数
    int maxCnt = INT_MIN;
    int begin = 0;//标记要截取的结果字符串的首字符在s中的位置
    while(back < length)
    {
        if(s[back] == 'G' || s[back] == 'C'){
            ++cnt;
        }
        if(back-front+1 == N)
        {
            if(cnt > maxCnt){
                begin = front;
                maxCnt = cnt;
            }
            if(s[front] == 'G' || s[front] == 'C'){
                --cnt;
            }
            ++front;
        }
        ++back;
    }
    cout << s.substr(begin,N);
}

//牛牛冲钻五
int main()
{
    int T = 0;
    cin >> T;
    while(T--)
    {
        int n = 0,k = 0;
        cin >> n >> k;
        string s(n,0);
        for(int i = 0;i < n;++i)
        {
            cin >> s[i];
        }
        int cnt = 0;
        int res = 0;
        for(int j = 0;j < n;++j)
        {
            if(s[j] == 'W'){
                ++cnt;
                if(cnt >= 3){
                    res += k;
                }
                else{
                    res += 1;
                }
            }
            else{
                cnt = 0;
                res -= 1;
            }
        }
        cout << res << endl;
    }
}
